<?php
class Warehouse_Model_TakeStock
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Warehouse_Model_DbTable_TakeStock();
	}
	
	/**
	*add DB Adapter
	*/
	public function getAdapter(){
		return $this->_table->getAdapter();
	}
	
	
	/**
	*Create A Record  
	*@param rowSet $row warehouse's data 
	*@return boolean
	*/
	public  function add($row){	 
 		return $this->_table->insert($row); 
	}
	
	/**
	*Update One Row  
	*@param rowSet $row warehouse's data 
	*@param int $wh_id 
	*@return boolean
	*/
	public  function update($row,$id)
	{	
		$where = $this->_table->getAdapter()->quoteInto("warehouse_id= ?", $id);
		return $this->_table->update($row, $where);		
	}
	
	public function updateByField($row,$field,$value)
	{ 
		$where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
		return $this->_table->update($row, $where);
	}
		
	/**
	*Delete One Row
	*@return boolean
	*/
	public function deleteByField($field,$value){	
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value); 
		return $this->_table->delete($where);		
	}

    public function getByCond($condition=array(),$type='*',$pageSize=0,$page=1,$orderby=array()){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
		if(isset($condition['ts_code']) && $condition['ts_code']!=''){
            $select->where('ts_code=?', $condition['ts_code']);
        }
        if(isset($condition['ts_status']) && $condition['ts_status']){
            $select->where('ts_status=?', $condition['ts_status']);
        }
        if(isset($condition['create']) && $condition['create']!=''){
            $mindate = date('Y-m-d',strtotime('-30 days')).' 00:00:00';
            $maxdate = date('Y-m-d').' 23:59:59';
            $select->where("ts_creat_time > ?",$mindate);
            $select->where("ts_creat_time < ?",$maxdate);
        }
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0){
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
//			print_r($sql);exit();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
	}
	
	public function getByfield($field,$value){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);	
		return $this->_table->fetchRow($where);
	}

	/**
	 * 获取当天最后一个 退货code
	 */
	public function getCurdateLastCode(){
		$select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'max(ts_code)');
        $mindate = date('Y-m-d').' 00:00:00';
        $maxdate = date('Y-m-d').' 23:59:59';

        $select->where("ts_creat_time > ?",$mindate);
        $select->where("ts_creat_time < ?",$maxdate);
		return $this->_table->getAdapter()->fetchOne($select);
	}
	
}